<?php

/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 上午10:42
 */
namespace app\src\filiation\action;

use app\src\account\enmu\AccountEnmu;
use app\src\accountScore\logic\AccountScoreLogic;
use app\src\base\action\BaseAction;
use app\src\filiation\logic\FiliationLogic;
use app\src\profit\logic\ProfitLogic;
use app\src\seller\logic\SellerLogic;
use app\src\seller\logic\SellerOrderLogic;
use app\src\seller\model\Seller;
use app\src\seller\model\SellerOrder;

class FiliationAction extends BaseAction
{
    const SELLER = 'p2';
    const PERSON = 'p3';
    const CITY = 'p4';
    const PROVINCE = 'p5';
    const  PERSON_TYPE='1';
    const SELLER_TYPE ='2';
    const  CITY_TYPE='3';
    const PROVINCE_TYPE ='4';
    const MASTER_TYPE ='5';
    const USER_TYPE ='6';

    /**
     * 根据邀请码添加用户父子关系
     * @param $uid
     * @param $invite_code
     * @return array
     */
    public function addByInviteCode($uid, $invite_code)
    {
        $result = (new FiliationLogic)->addByInviteCode($uid, $invite_code);
        return $this->result($result);
    }


    /*
     * 商户佣金总数与会员总数
     */

    public function memberAndMoney($id,$type){
        $income = 0;
        if($type == FiliationAction::SELLER_TYPE){
            $map=['p2'=>$id];
            $member_count = (new FiliationLogic())->count($map,'uid');
            if(!$member_count['status']) return $this->error($member_count['info']);
            $account_map=['p2'=>$id];
            $account = (new ProfitLogic())->sum($account_map,'s2');
            if(!$account['status']) return $this->error($account['info']);
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::SELLER, $id);
            $account['info'] = $score['info']['score1'];
            //查询每日实收金额
            $starttime = strtotime(date('Y-m-d'));
            $endtime = time();
            $order_map['create_time'] = ['between',[$starttime,$endtime]];
            $order_map['seller_id']=$id;
            $income = (new SellerOrder())->where($order_map)->sum('income');

        }
        if($type == FiliationAction::PERSON_TYPE){
            $map=['p3'=>$id];
            $member_count = (new FiliationLogic())->count($map,'uid');
            if(!$member_count['status']) return $this->error($member_count['info']);
            $account_map=['p4'=>$id];
            $account = (new ProfitLogic())->sum($account_map,'s4');
            if(!$account['status']) return $this->error($account['info']);
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::PLATFORM, $id);
            $account['info'] = $score['info']['score1'];
        }
        if($type == FiliationAction::CITY_TYPE){
            $map=['p4'=>$id];
            $member_count = (new FiliationLogic())->count($map,'uid');

            if(!$member_count['status']) return $this->error($member_count['info']);
            $account_map=['p5'=>$id];
            $account = (new ProfitLogic())->sum($account_map,'s5');
            if(!$account['status']) return $this->error($account['info']);
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::PLATFORM, $id);
            $account['info'] = $score['info']['score1'];
        }
        if($type == FiliationAction::PROVINCE_TYPE){
            $map=['p5'=>$id];
            $member_count = (new FiliationLogic())->count($map,'uid');
            if(!$member_count['status']) return $this->error($member_count['info']);
            $account_map=['p6'=>$id];
            $account = (new ProfitLogic())->sum($account_map,'s6');
            if(!$account['status']) return $this->error($account['info']);
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::PLATFORM, $id);
            $account['info'] = $score['info']['score1'];
        }
        if($type == FiliationAction::MASTER_TYPE){
            $account_map=['p3'=>$id];

            $seller = (new SellerLogic())->getInfo(['waiter_id'=>$id]);
            if(!empty($seller['info'])){
                $seller_id = $seller['info']['id'];
                $map=['p2'=>$seller_id];
                $member_count = (new FiliationLogic())->count($map,'uid');
                if(!$member_count['status']) return $this->error($member_count['info']);

            }
            $account = (new ProfitLogic())->sum($account_map,'s3');
            if(!$account['status']) return $this->error($account['info']);
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::USER, $id);
            $account['info'] = $score['info']['score1'];
        }
        if($type == FiliationAction::USER_TYPE){
            $map=['p1|p1_5'=>$id];
            $member_count = (new FiliationLogic())->count($map,'uid');
            if(!$member_count['status']) return $this->error($member_count['info']);
            $account_map=['p1'=>$id];
            $account_1 = (new ProfitLogic())->sum($account_map,'s1');
            $account_1_5 = (new ProfitLogic())->sum($account_map,'s1_5');

            if(!$account_1['status']) return $this->error($account['info']);
            $account=['status'=>true,'info'=>$account_1['info']+$account_1_5['info']];
            //查询当前积分
            $score = (new AccountScoreLogic)->getScore(AccountEnmu::USER, $id);
            $account['info'] = $score['info']['score1'];
        }


        if(empty($account['info'])) $account['info']='0';
        if(empty($income)) $income = 0;
        if(empty($member_count['info']))$member_count['info']='0';
        if($type == FiliationAction::SELLER_TYPE)return $this->success(['member_count'=>$member_count['info'],'account_sum'=>$account['info'],'income'=>$income]);
        return $this->success(['member_count'=>$member_count['info'],'account_sum'=>$account['info']]);

    }

    /**
     * 上下级关系
     * @param $uid
     * @param $type
     * @return array
     */

    public function getrelationship($uid,$type,$page){
        $res = (new FiliationLogic())->queryMembersziji($uid,$type,$page);
        return $this->success($res);

    }

}